﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kindohm.KSynth.Library;

namespace Kindohm.KSynth.UI
{
    public partial class VoiceControls : UserControl
    {
        WaveFormInfo selectedWaveForm;

        public VoiceControls()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VoiceController_Loaded);
            this.levelSlider.ValueChanged += new EventHandler(levelSlider_ValueChanged);
            this.panSlider.ValueChanged += new EventHandler(panSlider_ValueChanged);
            this.detuneSlider.ValueChanged += new EventHandler(detuneSlider_ValueChanged);
            this.phaseSlider.ValueChanged += new EventHandler(phaseSlider_ValueChanged);
        }

        public double Phase
        {
            get { return this.phaseSlider.Value; }
            set { this.phaseSlider.Value = value; }
        }

        public double Pan
        {
            get { return this.panSlider.Value; }
            set { this.panSlider.Value = value; }
        }

        public double Level
        {
            get { return this.levelSlider.Value; }
            set { this.levelSlider.Value = value; }
        }

        public double Detune
        {
            get { return this.detuneSlider.Value; }
            set { this.detuneSlider.Value = value; }
        }

        void phaseSlider_ValueChanged(object sender, EventArgs e)
        {
            if (this.PhaseChanged != null)
                this.PhaseChanged(this, EventArgs.Empty);
        }

        void detuneSlider_ValueChanged(object sender, EventArgs e)
        {
            if (this.DetuneChanged != null)
                this.DetuneChanged(this, EventArgs.Empty);
        }

        void panSlider_ValueChanged(object sender, EventArgs e)
        {
            if (this.PanChanged != null)
                this.PanChanged(this, EventArgs.Empty);
        }

        void levelSlider_ValueChanged(object sender, EventArgs e)
        {
            if (this.LevelChanged != null)
                this.LevelChanged(this, EventArgs.Empty);
        }

        public WaveFormInfo SelectedWaveForm
        {
            get { return this.selectedWaveForm; }
            set
            {
                this.selectedWaveForm = value;
                this.OnSelectedWaveformChanged();
            }
        }

        public string Title
        {
            get { return this.title.Text; }
            set { this.title.Text = value; }
        }

        void VoiceController_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (UIElement item in this.waveformButtonPanel.Children)
            {
                if (item is ToggleButton)
                {
                    ((ToggleButton)item).Checked += new RoutedEventHandler(item_Checked);
                    ((ToggleButton)item).Unchecked += new RoutedEventHandler(VoiceControls_Unchecked);
                }
            }
            this.sawButton.IsChecked = true;
        }

        void VoiceControls_Unchecked(object sender, RoutedEventArgs e)
        {
            int count = 0;
            foreach (UIElement item in this.waveformButtonPanel.Children)
            {
                if (item is ToggleButton)
                {
                    ToggleButton button = (ToggleButton)item;
                    if (button.IsChecked == true)
                        count++;
                }
            }
            if (count == 0)
            {
                ToggleButton button = (ToggleButton)sender;
                button.IsChecked = true;
            }
        }

        void item_Checked(object sender, RoutedEventArgs e)
        {
            foreach (UIElement item in this.waveformButtonPanel.Children)
            {
                if (item is ToggleButton)
                    if (item != sender)
                        ((ToggleButton)item).IsChecked = false;
                this.OnSelectedWaveformChanged((ToggleButton)sender);
            }
        }

        void OnSelectedWaveformChanged()
        {
            if (this.selectedWaveForm.WaveForm is SineWaveForm)
                this.sinButton.IsChecked = true;
            else if (this.selectedWaveForm.WaveForm is SquareWaveForm)
                this.squareButton.IsChecked = true;
            else if (this.selectedWaveForm.WaveForm is TriangleWaveForm)
                this.triButton.IsChecked = true;
            else if (this.selectedWaveForm.WaveForm is SawWaveForm)
                this.sawButton.IsChecked = true;
            else if (this.selectedWaveForm.WaveForm is NoiseWaveForm)
                this.noiseButton.IsChecked = true;
        }

        void OnSelectedWaveformChanged(ToggleButton button)
        {
            if (button == this.sinButton)
                this.selectedWaveForm = WaveFormRepository.Instance.FindByName("sin");
            else if (button == this.sawButton)
                this.selectedWaveForm = WaveFormRepository.Instance.FindByName("saw");
            else if (button == this.triButton)
                this.selectedWaveForm = WaveFormRepository.Instance.FindByName("tri");
            else if (button == this.squareButton)
                this.selectedWaveForm = WaveFormRepository.Instance.FindByName("sqr");
            else if (button == this.noiseButton)
                this.selectedWaveForm = WaveFormRepository.Instance.FindByName("noise");
            else
                this.selectedWaveForm = null;
            if (this.SelectedWaveFormChanged != null)
                this.SelectedWaveFormChanged(this, EventArgs.Empty);
        }

        public event EventHandler SelectedWaveFormChanged;
        public event EventHandler PanChanged;
        public event EventHandler DetuneChanged;
        public event EventHandler PhaseChanged;
        public event EventHandler LevelChanged;

    }
}
